package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.payconfig.EntityDishesInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.GetDishesPriceInfoListRequest;
import com.ocom.common.response.payconfig.OrderSummaryVo;
import com.ocom.common.utils.StringUtil;
import com.ocom.common.vo.dishes.DishesInfoListVo;
import com.ocom.common.vo.payconfig.GetDishesPriceInfoListVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoDishesInfo;
import com.ocom.payconfig.service.DishesInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishesInfoServiceImpl extends ServiceImpl<IDaoDishesInfo, EntityDishesInfo> implements DishesInfoService {

    @Autowired
    private IDaoDishesInfo iDaoDishesInfo;

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityDishesInfo> selectDishesInfoList(Long comId, JSONObject jsonObject) {
        PageHelper.startPage(jsonObject.getInteger("page"), jsonObject.getInteger("size"));
        QueryWrapper<EntityDishesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", jsonObject.getLong("comId"));
        queryWrapper.eq("merch_id", jsonObject.getLong("merchId"));
        if (StringUtil.isNotEmpty(jsonObject.getString("classIds"))) {
            String[] arr = jsonObject.getString("classIds").split(",");
            queryWrapper.in("dishes_class_id", arr);
        }
        if (jsonObject.getString("dishesName") != null) {
            queryWrapper.like("dishes_name", jsonObject.getString("dishesName"));
        }
        if (jsonObject.getString("dishesCode") != null) {
            queryWrapper.eq("dishes_code", jsonObject.getString("dishesCode"));
        }
        if (jsonObject.getInteger("status") != null && jsonObject.getInteger("status") != 0) {
            queryWrapper.eq("status", jsonObject.getInteger("status"));
        }
        queryWrapper.orderByAsc("dishes_class_id", "sort");
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public List<DishesInfoListVo> getAllDishes(Long comId) {
        return iDaoDishesInfo.getAllDishes(comId);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public List<DishesInfoListVo> selectDishesInfoList(Long comId, Integer merchId, List<String> classIds, String dishesName, String dishesCode, Integer status) {
        return iDaoDishesInfo.selectDishesInfoList(comId, merchId, classIds, dishesName, dishesCode, status);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesInfo selectDishesInfo(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntityDishesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", jsonObject.getLong("comId"));
        queryWrapper.eq("merch_id", jsonObject.getLong("merchId"));
        queryWrapper.and(wrapper -> wrapper.eq("dishes_name", jsonObject.getString("dishesName"))
                .or().eq("dishes_code", jsonObject.getString("dishesCode")));
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    @Override
    public EntityDishesInfo  insertDishesInfo(Long comId, EntityDishesInfo entityDishesInfo) {
         this.save(entityDishesInfo);
         return entityDishesInfo;
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesInfo selectUpdateDishes(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntityDishesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", jsonObject.getLong("comId"));
        queryWrapper.eq("merch_id", jsonObject.getLong("merchId"));
        queryWrapper.ne("dishes_id", jsonObject.getLong("dishesId"));
        queryWrapper.and(wrapper -> wrapper.eq("dishes_name", jsonObject.getString("dishesName"))
                .or().eq("dishes_code", jsonObject.getString("dishesCode")));
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean updateDishesInfo(Long comId, EntityDishesInfo entityDishesInfo) {
        return this.updateById(entityDishesInfo);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesInfo selectRemoveDishesDishes(Long comId, Integer merchId, Integer dishesClassId) {
        QueryWrapper<EntityDishesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("merch_id", merchId);
        queryWrapper.eq("dishes_class_id", dishesClassId);
        queryWrapper.eq("status", 1);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityDishesInfo> selectDishesInfoList(Long comId) {
        QueryWrapper<EntityDishesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", comId);
        return this.list(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDishesInfo sortMax(Long comId, JSONObject jsonObject) {
        QueryWrapper<EntityDishesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(sort) as sort");
        queryWrapper.eq("com_id", jsonObject.getLong("comId"));
        queryWrapper.eq("merch_id", jsonObject.getLong("merchId"));
        queryWrapper.eq("dishes_class_id", jsonObject.getLong("dishesClassId"));
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityDishesInfo> findByComIdAndDishesId(Long comId, List<Integer> dishesIds) {
        QueryWrapper<EntityDishesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", comId);
        queryWrapper.in("dishes_id", dishesIds);
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<OrderSummaryVo.DishesList> getDishesList(Long companyId, List<Integer> dishesIds) {
        return iDaoDishesInfo.getDishesList(companyId, dishesIds);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<GetDishesPriceInfoListVo> getDishesPriceInfoList(Long companyId, GetDishesPriceInfoListRequest getDishesPriceInfoListRequest) {
        if(ObjectUtil.isEmpty(getDishesPriceInfoListRequest.getStrClass())){
            return new ArrayList<>();
        }
        QueryWrapper<GetDishesPriceInfoListVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("D.com_id",getDishesPriceInfoListRequest.getComId())
                .eq("D.merch_id",getDishesPriceInfoListRequest.getMerchId())
                .eq("D.status",1)
                .in("D.dishes_class_id",getDishesPriceInfoListRequest.getStrClass())
                .eq("P.com_id",getDishesPriceInfoListRequest.getComId())
                .eq("P.merch_id",getDishesPriceInfoListRequest.getMerchId())
                .eq("P.point_id",0)
                .apply("P.dishes_id=D.dishes_id")
                .orderByDesc("D.dishes_class_id ","D.sort","D.dishes_id","P.price_mode");
        return this.iDaoDishesInfo.getDishesPriceInfoList(queryWrapper);
    }
}
